<?php
namespace App\Http\Controllers\Exchange;
use App\Http\Controllers\BaseApiController;
use Illuminate\Http\Request;
use App\Services\Exchange\Exchange;
class ExchangeController extends BaseApiController
{
    public $exchange;
    public function __construct(Exchange $exchange)
    {
        $this->exchange = $exchange;
    }
    /**
     * @param Request $request
     * @return array|void 兑换商品
     */
    public function exchange(Request $request){

        //接收要兑换的商品id
        $goods_id =  $request->post('goods_id');
        //接收要兑换的商品个数
        $num =  $request->post('num');
        //验证数据
        $data['goods_id'] = $num;
        $data['num'] = $goods_id;
        $rule = ['goods_id'=>'required|integer','num'=>'required|integer'];
        $tips= ['goods_id.required'=>'不能为空','goods_id.integer'=>'整形','num.required'=>'不能为空','num.integer'=>'整形',];

        $val = $this->validator($data,$rule,$tips);
        if($val->fails()){
            $error = $val->errors();
            return $error;
        }
        $uid = 2;
        $res =  $this->exchange->exchange($goods_id,$num,$uid);
        //判断用户积分是否够用
        if($res=='error'){
            return $this->result('40002','用户积分不够','error');
        }
        return $this->result('40000','兑换成功','success');
    }
    /**
     * 查询兑换商品日志
     */
    public function exchangeLog(){
        $uid = 2;
        return $this->exchange->exchangeLog($uid);
    }
}